﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;

namespace PTA.Image
{
    /// <summary>
    /// 图片缩放配置信息
    /// </summary>
    public class ImageZoomSetting
    {
        private string name = string.Empty;
        /// <summary>
        /// 配置标识
        /// </summary>
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }

        private int width = 0;
        /// <summary>
        /// 目标图片宽度
        /// </summary>
        public int Width
        {
            get
            {
                return this.width;
            }
            set
            {
                this.width = value;
            }
        }

        private int height = 0;
        /// <summary>
        /// 目标图片高度
        /// </summary>
        public int Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.height = value;
            }
        }

        private int orix = 0;
        /// <summary>
        /// 原始图片水平起始位置(默认:0)
        /// </summary>
        public int OriX
        {
            get
            {
                return this.orix;
            }
            set
            {
                this.orix = value;
            }
        }

        private int oriy = 0;
        /// <summary>
        /// 原始图片垂直起始位置(默认:0)
        /// </summary>
        public int OriY
        {
            get
            {
                return this.oriy;
            }
            set
            {
                this.oriy = value;
            }
        }

        private int oriwidth = int.MaxValue;
        /// <summary>
        /// 原始图片水平宽度
        /// </summary>
        public int OriWidth
        {
            get
            {
                return this.oriwidth;
            }
            set
            {
                this.oriwidth = value;
            }
        }

        private int oriheight = int.MaxValue;
        /// <summary>
        /// 原始图片水平高度
        /// </summary>
        public int OriHeight
        {
            get
            {
                return this.oriheight;
            }
            set
            {
                this.oriheight = value;
            }
        }

        private ZoomType zoom = ZoomType.按照给定的比例缩放;
        /// <summary>
        /// 图片缩放类型
        /// </summary>
        public ZoomType Zoom
        {
            get
            {
                return zoom;
            }
            set
            {
                zoom = value;
            }
        }

        private Color bgcolor = Color.Transparent;
        /// <summary>
        /// 目标图片的背景色
        /// </summary>
        public Color BgColor
        {
            get
            {
                return this.bgcolor;
            }
            set
            {
                this.bgcolor = value;
            }
        }

        private ImageFormat format = ImageFormat.Jpeg;
        /// <summary>
        /// 目标图片格式
        /// </summary>
        public ImageFormat Format
        {
            get
            {
                return format;
            }
            set
            {
                format = value;
            }
        }

        private int innerWidth = 0;
        /// <summary>
        /// 目标图片内边框宽度
        /// </summary>
        public int InnerBorderWidth
        {
            get
            {
                return innerWidth;
            }
            set
            {
                innerWidth = value;
            }
        }

        private Color innerColor = Color.White;
        /// <summary>
        /// 目标图片内边框颜色
        /// </summary>
        public Color InnerBorderColor
        {
            get
            {
                return this.innerColor;
            }
            set
            {
                this.innerColor = value;
            }
        }

        private int outerWidth = 0;
        /// <summary>
        /// 目标图片外边框宽度
        /// </summary>
        public int OuterBorderWidth
        {
            get
            {
                return this.outerWidth;
            }
            set
            {
                this.outerWidth = value;
            }
        }

        private Color outerColer = Color.White;
        /// <summary>
        /// 目标图片外边框颜色
        /// </summary>
        public Color OuterBorderColor
        {
            get
            {
                return this.outerColer;
            }
            set
            {
                this.outerColer = value;
            }
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ImageZoomSetting()
        {
        }

        internal ImageZoomSetting(Configuration.ImgZoomElement z)
        {
            this.name = z.Name;
            this.width = z.Width;
            this.height = z.Height;
            this.orix = z.OriX;
            this.oriy = z.OriY;
            this.oriwidth = z.OriWidth;
            this.oriheight = z.OriHeight;
            this.zoom = z.Zoom;
            this.bgcolor = z.BgColor;
            this.format = z.Format;
            this.innerWidth = z.InnerBorderWidth;
            this.innerColor = z.InnerBorderColor;
            this.outerWidth = z.OuterBorderWidth;
            this.outerColer = z.OuterBorderColor;
        }

        /// <summary>
        /// 从配置文件生成缩放信息
        /// </summary>
        /// <returns></returns>
        public static List<ImageZoomSetting> GetConfigSetting()
        {
            List<ImageZoomSetting> result = new List<ImageZoomSetting>();
            PTA.Image.Configuration.ImgZoomSection zoomsection = (PTA.Image.Configuration.ImgZoomSection)System.Configuration.ConfigurationManager.GetSection("zoom");
            if (zoomsection != null && zoomsection.Zooms != null)
            {
                foreach (PTA.Image.Configuration.ImgZoomElement zoom in zoomsection.Zooms)
                {
                    result.Add(new ImageZoomSetting(zoom));
                }
            }
            return result;
        }

        /// <summary>
        /// 生成字符串
        /// </summary>
        /// <returns>返回生成字符串后的值</returns>
        public override string ToString()
        {
            return string.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}_{8}_{9}_{10}", this.width, this.height, this.orix, this.oriy, this.oriwidth, this.oriheight,
               (int)this.zoom, this.innerWidth, this.innerColor.ToArgb(), this.outerWidth, this.outerColer.ToArgb());
        }
    }
}
